package yc.gg.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yc.gg.domain.dto.CreatePayRequestDTO;
import yc.gg.domain.req.ShopCartReq;
import yc.gg.domain.res.PayOrderRes;
import yc.gg.service.IOrderService;
import yc.gg.utils.Result;
import yc.gg.utils.UserHolder;

/**
 * \* User: Fuyue
 * \* Date: 2025/6/6
 * \* Description:
 * \
 */
@RestController
@RequestMapping("/api/order")
@Slf4j
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @PostMapping("/create_pay_order")
    public Result createPayOrder(@RequestBody CreatePayRequestDTO createPayRequestDTO){
        String openid = UserHolder.getUser().getOpenid();
        createPayRequestDTO.setUserId(openid);
        try{
            log.info("下单，创建订单开始 userId:{} productId:{}", createPayRequestDTO.getUserId(), createPayRequestDTO.getAnimalsId());
            String userId = createPayRequestDTO.getUserId();
            String getAnimalsId = createPayRequestDTO.getAnimalsId();
            //下单
            PayOrderRes payOrderRes = orderService.createOrder(ShopCartReq.builder()
                    .userId(userId)
                    .animalsId(getAnimalsId)
                    .build());
            log.info("下单，创建订单完成 userId:{} productId:{} orderId:{}", userId, getAnimalsId, payOrderRes.getOrderId());
            return Result.success(payOrderRes);
        } catch (Exception e) {
            log.error("下单，创建订单失败 userId:{} productId:{}", createPayRequestDTO.getUserId(), createPayRequestDTO.getAnimalsId(), e);
            return Result.error("订单创建失败");
            }
        }

        @PostMapping("/ensure")
        public Result ensure(@RequestBody String orderId){return orderService.ensure(orderId);}

        @GetMapping("/mine")
        public Result mine(){return orderService.mine();}
    }
